#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QVector>
#include <QtNetwork>
#include "connection.h"
#include "clientmonitor.h"
class Client:public QObject
{
    Q_OBJECT
public:
    explicit Client(QObject *parent = 0);
    static QString getIp();
signals:
    void recieveData(QString ip,QByteArray data);
    void notify(QString ip,int code);

    // server signals
    void recieveServerData(QByteArray data);
    void serverNotify(int code);
public slots:
    void onSendData(QString ip,QByteArray data);// send the data to the connection specified by ip
    void onCloseConnection(QString ip);//finish chating with someone,close
                                       //the connection specified by ip
    // server slots
    void onSendServerData(QByteArray data);
    void onCloseServerConnection();

private slots:
    void onNewConnection(Connection *conn);// when the ClientMonitor discover a new connection
    void onNotify(QString ip,int code);

    // private server slots
    void onRecieveServerData(QString ip,QByteArray data);
    void onServerNotify(QString ip,int code);
private:
    void appendConnection(Connection *conn);
private:
    ClientMonitor *clientMonitor;
    QVector<Connection *> conns;
    Connection *server_conn;
    int getConnIndex(QString ip);
    int tryCreateConnection(QString ip);
    bool connectToServer();

};

#endif // CLIENT_H
